﻿<cfscript>
/** 
* DataBase Frameworks - Sequence
* 
* @hint "DataBase Frameworks - Sequence - CourseBook" 
*/ 

component 
	displayname="public.service.db.senate.sequence.CourseBook" 
	output=false 
	accessors=true 
	{

	property type="string" name="TermID" hint="学期";
	property type="string" name="CourseID" hint="课程ID";
	property type="string" name="BookIndex" hint="图书征订计划序号";

	/**
	* @hint "初始化对象"
	*/
	public function init() output=false {
		return this;
	}
	
	
	/**
	* @hint "生成课程教材序列"
	*/
	public string function getBookIDX() output=false {
		/* 
			课程教材序列规则 1位代码
		*/
		var nextID = "0";

		sql = "	SELECT
					cb.bidx
				FROM
					t_course_book cb
				WHERE
					cb.term_id = :termId AND cb.cid = :courseId 
				ORDER BY
					cb.bidx DESC";
		
		queryObj = new Query(datasource = application.dnsMaster, maxRows = 1);
		queryObj.addParam(name = "termId", value = getTermID(), cfsqltype = "cf_sql_char");
		queryObj.addParam(name = "CourseID", value = getCourseID(), cfsqltype = "cf_sql_varchar");
		rs_check = queryObj.execute(sql = sql).getResult();
		
		if (rs_check.recordCount) {
			/* 创建一个追加id */
			nextID = rs_check.bidx + 1;
		}

		return nextID;
		
	}
	
	/**
	* @hint "自定义主键规则"
	*/
	public string function getID() output=false {
		
		var nextID = getTermID() & "," & getCourseID() & "," & getBookIndex();

		/* 
			主键生成规则 
			
			教材选用编号12位 3位学期代码, 6位本科课程号, 1位课程教材序列

			Unique: TERM_ID, CID, BIDX
		*/
		return nextID;
		
	}

}

</cfscript>